# represents Weinberg's tally.xls 
from Pid import *

#     def __init__(self, filename):
#         """ Returns a readexcel object of the specified filename - this may
#         take a little while because the file must be parsed into memory """
#         import xlrd
#         if not os.path.isfile(filename):
#             raise NameError, "%s is not a valid filename" % filename
#         self.__filename__ = filename
#         self.__book__ = xlrd.open_workbook(filename)
#         self.__sheets__ = {}
#         self.__sheetnames__ = []

class TallyPid:

    def __init__(self, filename):
        import xlrd
        import os.path
        if not os.path.isfile(filename):
            raise NameError, "'%s' is not a valid filename" % filename
        self.__filename__ = filename
        self.__book__ = xlrd.open_workbook(filename)
        self.__sheets__ = {}
        self.__sheetnames__ = []

    def hasPid(self, pid):
        # First, find a PID in any cell.
        # TODO: find PID only in first column
#        return(True)
        for sheetname in self.__book__.sheet_names():
            sheet = self.__book__.sheet_by_name(sheetname)
            for row in range(sheet.nrows):
                types,values = sheet.row_types(row),sheet.row_values(row)
                for val in values:
                    maybePid = Pid(val)
                    print "pid='%s', maybePid='%s', cmp=%d" % (pid.getStr(), maybePid.getStr(), cmp(pid.getStr(), maybePid.getStr()))
                    if maybePid.matches(pid):
                        return(True)
        return(False)
